package com.example.qjia.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.qjia.Listener.ModifyMessageListener;
import com.example.qjia.R;
import com.example.qjia.basic.BaseActivity;
import com.example.qjia.model.ModifyMesageModel;
import com.example.qjia.model.ModifyMessageModelImp;

import java.util.HashMap;
import java.util.Map;

public class ModifyMessageActivity extends BaseActivity implements View.OnClickListener,ModifyMessageListener{

    private LinearLayout backly;
    private LinearLayout commitly;
    private EditText modifyMessage;
    private EditText modifyPassword;

    private EditText surePassword;
    private int modifyId;
    private String m_message;
    private static final  String  TAG="ModifyMessageActivity";
    private Map<String,String>modifyParams;   //放置参数
    private ModifyMesageModel modifyMesageModel;


    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_modify_message;
    }

    @Override
    protected void init() {
        Intent intent=getIntent();
        modifyId=intent.getIntExtra("modifyId",0);
        m_message=intent.getStringExtra("modifyMessage");
        modifyParams=new HashMap<>();
        modifyMesageModel=new ModifyMessageModelImp();
        backly=(LinearLayout)findViewById(R.id.back_ly);
        backly.setOnClickListener(this);
        commitly=(LinearLayout)findViewById(R.id.commit_ly);
        commitly.setOnClickListener(this);
        modifyMessage=(EditText)findViewById(R.id.modify_message);
        modifyMessage.setText(m_message);
        modifyPassword=(EditText)findViewById(R.id.modify_password);
        surePassword=(EditText)findViewById(R.id.sure_password);
        Log.i(TAG, "init: "+modifyId);
        if(modifyId==3){
            modifyMessage.setVisibility(View.GONE);
            modifyPassword.setVisibility(View.VISIBLE);
            surePassword.setVisibility(View.VISIBLE);
        }else {
            modifyPassword.setVisibility(View.INVISIBLE);
            surePassword.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onClick(View view) {
        int viewID=view.getId();
        if(viewID==R.id.commit_ly) {
            if (modifyId == 1) {                     //修改邮箱的请求
                Log.i(TAG, "onClick: 修改邮箱");
                String modifyMail=modifyMessage.getText().toString();
                if(!modifyMail.equals("")){  //参数不等于空
                    Log.i(TAG, "onClick: 执行");

                    modifyParams.put("email",modifyMail); //只更改邮箱

                    modifyMesageModel.modifyStudentMailorPhone(modifyParams,this);     //
                }



            }
            if (modifyId == 2) {                    //修改的是电话号码的请求
                Log.i(TAG, "onClick: 修改电话号码");
                String modifyPhone=modifyMessage.getText().toString();
                if(!modifyPhone.equals("")){  //参数不等于空
                    modifyParams.put("phone",modifyPhone);//只更改电话号码
                    modifyMesageModel.modifyStudentMailorPhone(modifyParams,this);
                }

            }
            if (modifyId == 3) {                     //修改的是密码的请求

            }
        }
        if(viewID==R.id.back_ly){
            finish();

        }

    }

    @Override
    public void modifySuccess() {

        toast("更改成功！");
        finish();

    }

    @Override
    public void modifyFailed() {
        toast("更改失败！");

    }
}
